/*=============================================================================

  UI PARAMETER MODULE

  FILE: UIprm.cpp
  
  DESCRIPTION

    UI Parameter Module

  EXTERNALIZED FUNCTIONS


   Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

  EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who       what, where, why
  ---------  -------  -------------------------------------------------------
   03/14/04   nso4th    Initial version

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <Cust_AppDef.h>

/*=============================================================================
  CONSTANT DEFINITION
=============================================================================*/
#define UI_VERSION_LEN			(10)
#define UI_APPNAME_LEN			(4)

/*=============================================================================
  STRUCTURE DEFINITION
=============================================================================*/
/* Global UI Variable */
#pragma pack(1)
typedef struct
{
	//--- DEVICE.CONFIGURATION
	char				sysCFG[128];

	//--- CHECK.SUM
	word			wCheckSum;	
} UINV_s;
#pragma pack()

typedef struct
{
	char				appVER[128];
	dword			vwCUR;
} UISts_s;

static UINV_s	gNVprm;
static UISts_s	ui;

/*=============================================================================
  LOCALIZED FUNCION DEFINITION
=============================================================================*/
static void UIprm_Default(boolean bLoad);
static void UIprm_NVDefault(void);
static boolean UIprm_SetNV(dword dwType, dword wParam, dword lParam);
static dword UIprm_GetNV(dword dwType, dword wParam, dword lParam);

static dword ui_prmGetSysParamSize(void);
static void ui_prmSetSysFromSerial(byte *pSer, dword len);
static byte *ui_prmGetSysSerial(void);
static void ui_prmFreeSysSerial(byte *pSer);

/*=============================================================================
  EXTERNALIZED FUNCION DEFINITION
=============================================================================*/
void UIprm_Init(void)
{
	boolean		bLoad;
	char		*str;

	memZeros(&ui, sizeof(ui));
	memZeros(&gNVprm, sizeof(gNVprm));
	//Create Base Directory
	str = UIstring(NULL);
		FileOp_CreateDirectory(UIstring_TBL(UISTR_APP_PATH));
		FileOp_CreateDirectory(UIstring_TBL(UISTR_BIN_PATH));
		FileOp_CreateDirectory(UIstring_TBL(UISTR_SYS_PATH));
		//FileOp_CreateDirectory(UIstring_TBL(UISTR_LOG_PATH));
		//FileOp_CreateDirectory(UIstring_TBL(UISTR_LOGERR_PATH));
	UIstring(str);

	//File Load
	bLoad = UIprm_Load(NULL);
		
	UIprm_Default(bLoad);
}


void UIprm_Close(void)
{
	// TO DO : CUSTOM DEPENDENCY
	
	//UIprm_Save(NULL);
}

void UIprm_Default(boolean bLoad)
{
	UINV_s		*su = &gNVprm;

	if(!bLoad)
	{
		UIprm_NVDefault();
		UIprm_Save(NULL);
	}

#ifdef _ODEBUG_
	sprintf(ui.appVER, "%s.%002dD (Built on %s)", TG_APP_VERSION, TG_APP_BUILD, TG_APP_BUILD_DATE);
#else
	sprintf(ui.appVER, "%s.%002dR (Built on %s)", TG_APP_VERSION, TG_APP_BUILD, TG_APP_BUILD_DATE);
#endif

	ui.vwCUR = UI_NONE_VW;

}

void UIprm_NVDefault(void)
{
	UINV_s		*su = &gNVprm;
	dword		len;
	char			*str;
		
	str = UIstring(NULL);

	//---------------------------------------------------------------
	// DEFAULT HEADER
	//---------------------------------------------------------------
	//-- MAKE.VERSION Make Version Information
	AppUtil_GetAppName(str, UI_STRING_LEN);
	
	//---------------------------------------------------------------
	// APPLICATION CONFIGURATION
	//---------------------------------------------------------------
	//--- DEVICE
	sprintf(su->sysCFG, "%s\\%s.ini", UIstring_TBL(UISTR_SYS_PATH), str);

	//---------------------------------------------------------------
	// CHECK SUM
	//---------------------------------------------------------------
	{
		byte		*pBuf;

		pBuf = (byte*)&gNVprm;
		len = ui_prmGetSysParamSize() - sizeof(su->wCheckSum);
		su->wCheckSum = FileIo_CheckSum((word*)pBuf, len);
	}
	UIstring(str);
}

boolean UIprm_Set(dword dwType, dword wParam, dword lParam)
{
	if(dwType < UI_NVPARAM_END)
		return	UIprm_SetNV(dwType, wParam, lParam);

	switch(dwType)
	{
	case UI_VWCURRENT_P:
	{
		ui.vwCUR = wParam;
		break;
	}
	default:
		return	FALSE;
	}

	return	TRUE;
}

dword UIprm_Get(dword dwType, dword wParam, dword lParam)
{
	if(dwType < UI_NVPARAM_END)
		return	UIprm_GetNV(dwType, wParam, lParam);

	switch(dwType)
	{
	case UI_VERSION_SP:
	{
		strcpy((char*)wParam, &ui.appVER[0]);
		return	TRUE;
	}
	case UI_VWCURRENT_P:
	{
		return	ui.vwCUR;
	}
	default:
		return	FALSE;
	}
	
	return	TRUE;
}

boolean UIprm_SetNV(dword dwType, dword wParam, dword lParam)
{
	UINV_s		*su = &gNVprm;

	switch(dwType)
	{
	//--- DEVICE.CONFIGURATION
	case UI_SYSMGR_SP:
	{
		strcpy(su->sysCFG, (char*)wParam);
		break;
	}
	default:
		return	FALSE;
	}
	return	TRUE;
}

dword UIprm_GetNV(dword dwType, dword wParam, dword lParam)
{
	UINV_s		*su = &gNVprm;

	switch(dwType)
	{
	//--- DEVICE.CONFIGURATION
	case UI_SYSMGR_SP:
	{
		strcpy((char*)wParam, su->sysCFG);
		break;
	}
	default:
		return	FALSE;
	}

	return	TRUE;
}

boolean UIprm_Load(char *pszName)
{
	mcxt		pFile;
	UINV_s		*su = &gNVprm;
	byte		*pBuf;
	word		fSum, rSum;
	dword		len, len_r;
	boolean		bDone = FALSE;

	if(pszName == NULL)
	{
		char			*strFull, *strApp;

		strApp = UIstring(NULL);
		strFull = UIstring(NULL);
		AppUtil_GetAppName(strApp, UI_STRING_LEN);
		sprintf(strFull, "%s\\%s.cfg", UIstring_TBL(UISTR_SYS_PATH), strApp);
		pFile = FileIo_Open(strFull, FIO_OPEN_R);

		UIstring(strApp);
		UIstring(strFull);
	}
	else
	{
		pFile = FileIo_Open(pszName, FIO_OPEN_R);
	}
	
	if(pFile == NULL)
		return	bDone;

	if(FileIo_Size(pFile, &len) == TRUE)
	{
		pBuf = (byte*)memCalloc(len);
	}
	else
	{
		FileIo_Close(pFile);
		return	FALSE;
	}
	
	if(FileIo_Read(pFile, pBuf, len, &len_r))
	{
		if(len == len_r)
			bDone = TRUE;
	}

	// Check Version
	{
		// TO DO : CUSTOM DEPENDENCY
	}

	// Check-Sum
	if(bDone)
	{
		len = len - sizeof(su->wCheckSum);
		rSum = FileIo_CheckSum((word*)pBuf, len);
		fSum = *((word*)(pBuf + len));
		if(rSum != fSum)
		{
			bDone = FALSE;
		}
		len = len + sizeof(su->wCheckSum);
	}

	// Data length
	if(len != ui_prmGetSysParamSize())
	{
		memFree(pBuf);
		FileIo_Close(pFile);

		return	FALSE;
	}

	// Get System Parameter
	if(bDone)
	{
		ui_prmSetSysFromSerial(pBuf, len);
	}

	FileIo_Close(pFile);
	memFree(pBuf);

	return	bDone;
}


boolean UIprm_Save(char *pszName)
{
	mcxt		pFile;
	UINV_s		*su;
	static char		szFull[32], szApp[16];
	byte		*pBuf;
	dword		len, len_w;
	boolean		bDone = FALSE;

	if(pszName == NULL)
	{
		AppUtil_GetAppName(szApp, sizeof(szApp));
		sprintf(szFull, "%s\\%s.cfg", UIstring_TBL(UISTR_SYS_PATH), szApp);
		pFile = FileIo_Open(szFull, FIO_OPEN_WR);
	}
	else
	{
		pFile = FileIo_Open(pszName, FIO_OPEN_WR);
	}
	
	if(pFile == NULL)
		return	bDone;

	pBuf = ui_prmGetSysSerial();

	// Check-Sum
	su = &gNVprm;
	len = ui_prmGetSysParamSize() - sizeof(su->wCheckSum);
	su->wCheckSum = FileIo_CheckSum((word*)pBuf, len);
	
	len = len + sizeof(su->wCheckSum);
	if(FileIo_Write(pFile, pBuf, len, &len_w))
	{
		if(len == len_w)
			bDone = TRUE;
	}

	ui_prmFreeSysSerial(pBuf);
	FileIo_Close(pFile);

	return	bDone;
}
/*=============================================================================
  LOCALIZED FUNCION IMPLEMENTATION
=============================================================================*/
dword ui_prmGetSysParamSize(void)
{
	dword		Len = 0;

	Len = sizeof(UINV_s);

	// TO DO: CUSTOM DEPENDENCY

	return	Len;
}

void ui_prmSetSysFromSerial(byte *pSer, dword len)
{
	// TO DO: CUSTOM DEPENDENCY
	memCopy((byte*)&gNVprm, pSer, len);

	return;
}

byte *ui_prmGetSysSerial(void)
{
	// TO DO: CUSTOM DEPENDENCY

	return	(byte*)&gNVprm;
}

void ui_prmFreeSysSerial(byte *pSer)
{
	// TO DO: CUSTOM DEPENDENCY
	return;
}

/*--- END OF UIprm.CPP ---*/
